/*
 * 调拨审核
 * 参数   交易码：trancode=61116
 * 
 */

package org.ccams.tran;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.ccams.domain.TMaterielStock;
import org.ccams.domain.TpMovbackD;
import org.ccams.domain.User;
import org.ccams.web.UserSession;
import org.ccams.web.WebBeanFactory;
import org.json.JSONArray;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class Tran61116 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String params = (String) request.getParameter("detail");
		String uname = request.getParameter("uname");
		JSONArray detailParams = new JSONArray(params);
		
		Short astate = Short.parseShort(request.getParameter("astate"));//取得审核状态
		//获得审核人
		HttpSession session = request.getSession();
		UserSession userSession = (UserSession)session.getAttribute("userSession");
		User user = userSession.getUser();
		String userName=user.getUserName();
		//取得当前日期
		Date adate = new Date();
		
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		int k=0;		
		DataSourceTransactionManager txm = (DataSourceTransactionManager) WebBeanFactory.getBean("transactionManager",request);
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = txm.getTransaction(def);
		try{
			for(int i=0;i<detailParams.length();i++){
				org.json.JSONObject detail = detailParams.optJSONObject(i);
				TpMovbackD tmd = new TpMovbackD();
				tmd.setNum(detail.getLong("num"));
				tmd.setOrd(detail.getInt("ord"));
				tmd.setAname(userName);
				tmd.setAstate(astate);
				tmd.setAdate(adate);
				tmd.setAret(detail.getString("aret"));
				k = getCcamsService().getTpmovbackdDao().updateCommit(tmd);//更新审核信息
				
//				TMaterielStock tms = new TMaterielStock();
//				tms.setUname(uname);
//				tms.setMaterielName(detail.getString("materielName"));
//				tms.setAmount(0-detail.getLong("amount"));
//				k = getCcamsService().getTmaterielstockDao().updateStockAmount(tms);//减去调出部门物料数量
			}
		}
		catch(Exception ex){
			txm.rollback(status);
	        ex.printStackTrace();
		}finally{
			txm.commit(status);
		}
		if (k >= 1) {
			success = true;
			message = "审核成功";
		} else {
			success = false;
			message = "审核失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
